﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace Barbar.UrlGuard.Dal {
  public class AuthenticationWindowsIntegrated : AuthenticationBasic {
    private string m_Domain;
    private bool m_PassSpecifiedDomain;
    private bool m_UseCurrentUserCredentials;

    public bool UseCurrentUserCredentials {
      get { return m_UseCurrentUserCredentials; }
      set { m_UseCurrentUserCredentials = value; }
    }

    public string Domain {
      get { return m_Domain; }
      set { m_Domain = value; }
    }

    public bool PassSpecifiedDomain {
      get { return m_PassSpecifiedDomain; }
      set { m_PassSpecifiedDomain = value; }
    }

    public override int AuthenticationTypeID {
      get { return AuthenticationType.NTLM.AuthenticationTypeID; }
    }

    public override object Clone() {
      AuthenticationWindowsIntegrated result = new AuthenticationWindowsIntegrated();
      result.Domain = m_Domain;
      result.m_PassSpecifiedDomain = m_PassSpecifiedDomain;
      result.m_UseCurrentUserCredentials = m_UseCurrentUserCredentials;
      return result;
    }

    public override void SetWebRequest(WebRequest request) {
      if (request == null)
        throw new ArgumentNullException("request");

      if (UseCurrentUserCredentials)
        request.Credentials = CredentialCache.DefaultCredentials;
      else {
        if (!PassSpecifiedDomain) {
          request.Credentials = new NetworkCredential(UserName, Password);
        } else {
          request.Credentials = new NetworkCredential(UserName, Password, Domain);
        }
      }
    }
  }
}
